/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif

namespace Oceanside
{
    /// <summary>
    /// Interaction logic for ControlPanelWindowInstance.xaml
    /// </summary>
    public partial class SystemWindow : PosWindow
    {
        public const int CONST_ROWS_PER_VIEW = 16;
        private int mainPivotIndx = 0;

        static PageUser _pageUsers;
        static PageUserRoles _pageUserRoles;
        static PageAccessLevels _pageAccessLevels;
        static PageJobCodes _pageJobCodes;
        static PagePayGrade _pagePayGrades;
        static PageMenus _pageMenus;
        static PageForcedModifiers _pageForcedModGroups;
        static PageExceptionModifiers _pageExceptionModGroups;
        static PagePriceIntervals _pagePriceIntervals;
        static PageSystemConfiguration _pageSystemConfig;
        static PageKiosks _pageNodes;
        static PagePaymentGateway _pagePaymentGateway;
        //static PageMapKioskProdGroupToPrinter _pageProductGroupPrinter;
        static PageCompReason _pageCompReason;
        static PagePciLog _pageSysLogViewer;
        static PageDataTools _pageDatabaseTools;
        static PageLayout _pageLayout;
        static PageAutoGratuity _pageAutoGratuity;
        static PageKioskToAutoGratuityPairing _pageNodeToAutoGratuityPairing;
        static PageDayPart _pageDayParts;
        static PageHouseAccount _pageHouseAccount;
        //static PageVendorAccount _pageVendorAccount;
        static PageExitBlank _pageExitConfig;
        static PagePrinters _pagePrinters;
        static PageProducts _pageProducts;

        private static List<ButtonBackOfHouseHelp> mainCategoryButtonsList = null;
        private static List<Page> pagesList = null;

        public Image VirtualkeyBoard
        {
            get { return imageKeyboard; }
        }

        static PageLogin _pageLogin;
        static PageRootPassword _pageRootPassword;

        static bool _isBackOfficeMode = false;
        public static bool IsBackOfficeMode
        {
            get { return SystemWindow._isBackOfficeMode; }
            set { SystemWindow._isBackOfficeMode = value; }
        }

        static Page _currentPage = null;

        /// <summary>
        /// 
        /// </summary>
        private SystemWindow()
            : base()
        {
            this.InitializeComponent();

            mainCategoryButtonsList = new List<ButtonBackOfHouseHelp>(CONST_ROWS_PER_VIEW);
            mainCategoryButtonsList.Add(boh_0);
            mainCategoryButtonsList.Add(boh_1);
            mainCategoryButtonsList.Add(boh_2);
            mainCategoryButtonsList.Add(boh_3);
            mainCategoryButtonsList.Add(boh_4);
            mainCategoryButtonsList.Add(boh_5);
            mainCategoryButtonsList.Add(boh_6);
            mainCategoryButtonsList.Add(boh_7);
            mainCategoryButtonsList.Add(boh_8);
            mainCategoryButtonsList.Add(boh_9);
            mainCategoryButtonsList.Add(boh_10);
            mainCategoryButtonsList.Add(boh_11);
            mainCategoryButtonsList.Add(boh_12);
            mainCategoryButtonsList.Add(boh_13);
            mainCategoryButtonsList.Add(boh_14);
            mainCategoryButtonsList.Add(boh_15);

            foreach (ButtonBackOfHouseHelp boh in mainCategoryButtonsList)
            {
                boh.ShineButtonClicked += new RoutedEventHandler(boh_func_button_click);
            }

            buttonDown.ButtonDownDelegate += new ButtonBackOfHouseContent.FunctionButtonDownDelegate(ScrollDownClicked_NOTHROW);
            buttonUp.ButtonDownDelegate += new ButtonBackOfHouseContent.FunctionButtonDownDelegate(ScrollUpClicked_NOTHROW);

            _pageLogin = new PageLogin(new PageLogin.BohLoginDelegate(LoginCallBack_NOTHROW));
            _pageRootPassword = new PageRootPassword(ResetCallback_NOTHROW);

            PageImpBaseClass.HideKeyboard = new Action(HideKeyboard_NOTHROW);
            PageImpBaseClass.ShowKeyboard = new Action(ShowKeyboard_NOTHROW);

            this.Height = PosWindow.MainPosWindow.Height;
            this.Width = PosWindow.MainPosWindow.Width;
        }

        static SystemWindow singleton = null;
        static SystemWindow()
        {
            singleton = new SystemWindow();
        }
        public static SystemWindow Singleton
        {
            get
            {
                return singleton;
            }
        }

        private static bool _isConfigOnlyMode = false;
        public static bool IsConfigOnlyMode
        {
            get { return SystemWindow._isConfigOnlyMode; }
            set { SystemWindow._isConfigOnlyMode = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        void ScrollDownClicked_NOTHROW()
        {
            try
            {
                if (pagesList.Count > (mainPivotIndx + CONST_ROWS_PER_VIEW))
                {
                    mainPivotIndx += CONST_ROWS_PER_VIEW;
                    UpdateLastClickedButton(null);
                    SetMainCatDataContext();
                }
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_SYSWINDOW_SCROLL_DOWN_CLICK, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        void ScrollUpClicked_NOTHROW()
        {
            try
            {
                ///If there is more pivot room.
                if (mainPivotIndx > 0)
                {
                    ///If there is an entire panels worth of pivot
                    if ((mainPivotIndx - CONST_ROWS_PER_VIEW) >= 0)
                    {
                        mainPivotIndx -= CONST_ROWS_PER_VIEW;
                    }
                    else ///Its greater than zero, but not a panels worth. Set to zero.
                    {
                        mainPivotIndx = 0;
                    }
                    UpdateLastClickedButton(null);
                    SetMainCatDataContext();
                }
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_SYSWINDOW_SCROLL_UP_CLICK, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void SetMainCatDataContext()
        {
            int count = pagesList.Count - mainPivotIndx;

            for (int indx = 0; indx < CONST_ROWS_PER_VIEW; indx++)
            {
                if (count > indx)
                {
                    mainCategoryButtonsList[indx].Visibility = Visibility.Visible;
                    mainCategoryButtonsList[indx].DataContext = pagesList[indx + mainPivotIndx];
                }
                else
                {
                    mainCategoryButtonsList[indx].Visibility = Visibility.Hidden;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static void InitStatics()
        {
            PageImpBaseClass.EditFormButton = Singleton.buttonEdit;
            PageImpBaseClass.CancelEditButton = Singleton.buttonCancel;
            PageImpBaseClass.SaveFormChangesButton = Singleton.buttonSaveChanges;
            PageImpBaseClass.NewGroupButton = Singleton.buttonNewGroup;
            PageImpBaseClass.NewEntityButton = Singleton.buttonNewEntity;
            PageImpBaseClass.DeleteEntityButton = Singleton.buttonDeleteEntity;
            PageImpBaseClass.DeleteGroupButton = Singleton.buttonDeleteGroup;

            pagesList = new List<Page>();

            _pageExitConfig = new PageExitBlank();
            pagesList.Add(_pageExitConfig);

            _pageJobCodes = new PageJobCodes();
            pagesList.Add(_pageJobCodes);

            _pageAccessLevels = new PageAccessLevels();
            pagesList.Add(_pageAccessLevels);

            _pagePayGrades = new PagePayGrade();
            pagesList.Add(_pagePayGrades);

            _pageUsers = new PageUser();
            pagesList.Add(_pageUsers);

            _pageUserRoles = new PageUserRoles();
            pagesList.Add(_pageUserRoles);

            _pagePrinters = new PagePrinters();
            pagesList.Add(_pagePrinters);

            _pageForcedModGroups = new PageForcedModifiers();
            pagesList.Add(_pageForcedModGroups);

            _pageExceptionModGroups = new PageExceptionModifiers();
            pagesList.Add(_pageExceptionModGroups);

            _pagePriceIntervals = new PagePriceIntervals();
            pagesList.Add(_pagePriceIntervals);

            _pageProducts = new PageProducts();
            pagesList.Add(_pageProducts);

            _pageMenus = new PageMenus();
            pagesList.Add(_pageMenus);

            _pageAutoGratuity = new PageAutoGratuity();
            pagesList.Add(_pageAutoGratuity);

            _pageDayParts = new PageDayPart();
            pagesList.Add(_pageDayParts);

            _pagePaymentGateway = new PagePaymentGateway();
            pagesList.Add(_pagePaymentGateway);

            _pageSystemConfig = new PageSystemConfiguration();
            pagesList.Add(_pageSystemConfig);

            _pageNodes = new PageKiosks();
            pagesList.Add(_pageNodes);

            _pageNodeToAutoGratuityPairing = new PageKioskToAutoGratuityPairing();
            pagesList.Add(_pageNodeToAutoGratuityPairing);

            //_pageProductGroupPrinter = new PageMapKioskProdGroupToPrinter();
            //pagesList.Add(_pageProductGroupPrinter);

            _pageCompReason = new PageCompReason();
            pagesList.Add(_pageCompReason);

            _pageHouseAccount = new PageHouseAccount();
            pagesList.Add(_pageHouseAccount);

            //_pageVendorAccount = new PageVendorAccount();
            //pagesList.Add(_pageVendorAccount);

            _currentPage = _pageLogin;

            _pageSysLogViewer = new PagePciLog();
            pagesList.Add(_pageSysLogViewer);

            _pageDatabaseTools = new PageDataTools();
            pagesList.Add(_pageDatabaseTools);

            if (Constants.IsQuickServiceMode)
            {
                _pageLayout = new PageLayout();
                pagesList.Add(_pageLayout);
            }
        }

        private static Boolean isLoggedIn = false;

        /// <summary>
        /// 
        /// </summary>
        private static Action leaveSystemConfiguration = null;
        public static Action SwitchToPreviousScreenDelegate
        {
            get { return SystemWindow.leaveSystemConfiguration; }
            set { SystemWindow.leaveSystemConfiguration = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param Name="sender"></param>
        /// <param Name="e"></param>
        private static void ExitSystemConfig()
        {
            try
            {
                SwitchToPreviousScreenDelegate();
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_SYSWINDOW_CANCEL_MOUSEDOWN, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static void Cancel_NOTHROW()
        {
            try
            {
                if (_currentPage != null)
                {
                    if (_currentPage.CheckAccess())
                    {
                        try
                        {
                            if (PageImpBaseClass.Context != null)
                            {
                                (_currentPage as IPageToImp).Navigating();
                                PageImpBaseClass.Context.SaveChanges();
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                        }

                        ///If they are configuring the system, just exit.  If they completed the config,
                        ///the next boot will launch the full application.
                        if (_isConfigOnlyMode)
                        {
                            Environment.Exit(0);
                        }
                        else
                        {
                            isLoggedIn = false;
                            _currentPage = _pageLogin;
                        }
                    }
                    else
                    {
                        _currentPage.Dispatcher.Invoke(new Action(Cancel_NOTHROW));
                    }
                }
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_SYSWINDOW_CANCEL, exc);
            }
        }

        /// <summary>
        /// Used to hold the last help button that was last clicked so we can clear the highlight
        /// color.
        /// </summary>
        ButtonBackOfHouseHelp lastClickedHelpButton = null;


        /// <summary>
        /// 
        /// </summary>
        public override void PrepareForScreenChange()
        {
            actionButtonsPanel.Visibility = System.Windows.Visibility.Hidden;

            SHIFT.SetContextToAppendOnly();

            //_currentPage = _pageLogin;
            mainPivotIndx = 0;
            if (lastClickedHelpButton != null)
            {
                lastClickedHelpButton.ClearSelectedColor();
            }
            SetMainCatDataContext();
            AuthenticatedLaunch();
        }

        /// <summary>
        /// 
        /// </summary>
        public override void PrepareForScreenLeave()
        {
            SHIFT.GlobalContext.SaveChanges();
            SHIFT.SetContextToNoTracking();
        }

        /// <summary>
        /// 
        /// </summary>
        private void HideKeyboard_NOTHROW()
        {
            try
            {
                if (_currentPage is PageLogin && !isLoggedIn)
                {
                    ///If the current page is the login page and the user is not logged in, then do not let
                    ///other page code hide the keyboad.
                }
                else
                {
                    this.VirtualkeyBoard.Visibility = System.Windows.Visibility.Hidden;
                }
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_SYSWINDOW_HIDE_KEYBOARD, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void ShowKeyboard_NOTHROW()
        {
            try
            {
                this.VirtualkeyBoard.Visibility = System.Windows.Visibility.Visible;
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_SYSWINDOW_SHOW_KEYBOARD, exc);
            }
        }

        /// <summary>
        /// Will assure the user has permission to perform the desired action and will launch that
        /// page if they do.
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        private Boolean AuthenticatedLaunch()
        {
            Boolean retVal = false;

            if (_currentPage is PageLogin)
            {
                actionButtonsPanel.Visibility = System.Windows.Visibility.Hidden;
                retVal = true;
                if (DominicaEntities.HasBohUser(DominicaEntities.GetEfConString()) && !isLoggedIn)
                {
                    ShowKeyboard_NOTHROW();
                    _pageLogin.mainBorder.Visibility = System.Windows.Visibility.Visible;
                    isLoggedIn = false;
                }
                else
                {
                    HideKeyboard_NOTHROW();
                    _pageLogin.mainBorder.Visibility = System.Windows.Visibility.Hidden;
                    isLoggedIn = true;
                }
                (_currentPage as IPageToImp).ResetFocus();
                pageFrame.Navigate(_pageLogin);
            }
            else if (_currentPage is PageExitBlank)
            {
                ExitSystemConfig();
            }
            else if (isLoggedIn)
            {
                actionButtonsPanel.Visibility = System.Windows.Visibility.Visible;
                retVal = true;
                PageImpBaseClass.HideKeyboard();
                (_currentPage as IPageToImp).ResetFocus();
                pageFrame.Navigate(_currentPage);
            }
            else
            {
                UMD.ShowGuiMessageSTASafe_NOTHROW("You must first login to the system to perform that action.");
                (_currentPage as IPageToImp).ResetFocus();
                lastClickedHelpButton.ClearSelectedColor();
                lastClickedHelpButton = null;
            }
            return retVal;
        }

        /// <summary>
        /// 
        /// </summary>
        private void ResetCallback_NOTHROW()
        {
            try
            {
                isLoggedIn = true;
                _currentPage = _pageLogin;
                AuthenticatedLaunch();
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_SYSWINDOW_RESET_CALLBACK, exc);
            }
        }

        /// <summary>
        /// Called when the user attempts to login.  Used as a callback.
        /// </summary>
        /// <param name="isIn"></param>
        private void LoginCallBack_NOTHROW(PageLogin.LoginState state, string username)
        {
            try
            {
                if (state == PageLogin.LoginState.NotLoggedIn)
                {
                    (_currentPage as IPageToImp).ResetFocus();
                }
                else if (state == PageLogin.LoginState.Locked)
                {
                    (_currentPage as IPageToImp).ResetFocus();
                    System.Windows.MessageBox.Show("Your account has been locked out for the next 30 minutes for too many login failures.", "Account Locked", MessageBoxButton.OK);
                }
                else if (state == PageLogin.LoginState.PasswordReset)
                {
                    System.Windows.MessageBox.Show("You must reset your password to continue.", "Password Reset Required", MessageBoxButton.OK);
                    (_pageRootPassword as IPageToImp).SetResetUsername(username);
                    _currentPage = _pageRootPassword;
                    pageFrame.Navigate(_pageRootPassword);
                }
                else if (state == PageLogin.LoginState.LoggedIn)
                {
                    isLoggedIn = true;
                    AuthenticatedLaunch();
                }
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_SYSWINDOW_LOGIN_CALLBACK, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private static void WarnOfNavigatingAwayEvent()
        {
            ///Tell the current page we are navigating away.
            if (_currentPage != null)
            {
                (_currentPage as IPageToImp).Navigating();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void boh_func_button_click(object sender, RoutedEventArgs e)
        {
            try
            {
                e.Handled = true;
                ButtonBackOfHouseHelp currButton = (Oceanside.ButtonBackOfHouseHelp)e.OriginalSource;
                UpdateLastClickedButton(currButton);

                string indxString = (string)(currButton).Tag;

                int selectedIndex = 0;
                try
                {
                    selectedIndex = Convert.ToInt32(indxString);
                }
                catch (Exception) { }

                selectedIndex += mainPivotIndx;

                if (selectedIndex >= 0 && selectedIndex < pagesList.Count)
                {
                    WarnOfNavigatingAwayEvent();
                    _currentPage = pagesList[selectedIndex];
                    AuthenticatedLaunch();
                }
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_SYSWINDOW_BOH_FUNC_BUTTON_CLICK, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="justClickedButton"></param>
        private void UpdateLastClickedButton(ButtonBackOfHouseHelp justClickedButton)
        {
            ///Clear the highlight on the last clicked button.
            if (lastClickedHelpButton != null)
            {
                if (lastClickedHelpButton != justClickedButton)
                {
                    lastClickedHelpButton.ClearSelectedColor();
                }
            }
            lastClickedHelpButton = justClickedButton;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imageKeyboard_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_currentPage is PageLogin)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pageLogin.textBoxUsername, _pageLogin.passwordBoxPassword });
            }
            else if (_currentPage is PageJobCodes)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pageJobCodes.textBoxName });
            }
            else if (_currentPage is PageAccessLevels)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pageAccessLevels.textBoxName });
            }
            else if (_currentPage is PagePayGrade)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pagePayGrades.tbConfigName, _pagePayGrades.wageRateTextBox, _pagePayGrades.overtimeRateTextBox, _pagePayGrades.overtimeThresholdTextBox });
            }
            else if (_currentPage is PageUserRoles)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pageUserRoles.textBoxDisplayName });
            }
            else if (_currentPage is PageUser)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pageUsers.tblastNameTextBox, _pageUsers.tbfirstNameTextBox, _pageUsers.tbuserNameTextBox,
                _pageUsers.tbbohPasswordTextBox, _pageUsers.tbtextboxKeypadPassword, _pageUsers.tbmiddleNameTextBox,
                _pageUsers.tbcityTextBox,_pageUsers.tbstateTextBox, _pageUsers.tbzipcodeTextBox,
                _pageUsers.tbaddress1TextBox, _pageUsers.tbaddress2TextBox, _pageUsers.tbMobilePhoneTextBox,
                _pageUsers.tbtelephoneTextBox, _pageUsers.tbssnTextBox, _pageUsers.tbEmail, _pageUsers.tbCountry, _pageUsers.tbExternalPayrollId});
            }
            else if (_currentPage is PageRootPassword)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pageRootPassword.passwordBoxPassword, _pageRootPassword.passwordBoxPasswordConfirm,
                        _pageRootPassword.textBoxQuestion, _pageRootPassword.textBoxAnswer, _pageRootPassword.textBoxEmail });
            }
            else if (_currentPage is PagePaymentGateway)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pagePaymentGateway.tbMerchantId, _pagePaymentGateway.tbProcessorKey });
            }
            else if (_currentPage is PageExceptionModifiers)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pageExceptionModGroups.tbGroupName,  
                        _pageExceptionModGroups.tbGroupPrice,
                    _pageExceptionModGroups.tbGroupLocWeight,
                    _pageExceptionModGroups.tbDownTick,   
                    _pageExceptionModGroups.tbName,
                    _pageExceptionModGroups.tbPriceTextBox, 
                    _pageExceptionModGroups.textBoxPLU,
                    _pageExceptionModGroups.textBoxUPC, 
                    _pageExceptionModGroups.textBoxSku, 
                    _pageExceptionModGroups.tbForcedModMenuWeight,
                    _pageExceptionModGroups.tbCalorieCount});
            }
            else if (_currentPage is PageForcedModifiers)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pageForcedModGroups.tbGroupName, 
                        _pageForcedModGroups.tbMaxChoice, 
                        _pageForcedModGroups.tbMinChoice, 
                        _pageForcedModGroups.tbGroupPrice,
                    _pageForcedModGroups.tbGroupLocWeight,
                    _pageForcedModGroups.tbDownTick,   
                    _pageForcedModGroups.tbName,
                    _pageForcedModGroups.tbPriceTextBox, 
                    _pageForcedModGroups.textBoxPLU,
                    _pageForcedModGroups.textBoxUPC, 
                    _pageForcedModGroups.textBoxSku, 
                    _pageForcedModGroups.tbForcedModMenuWeight,
                    _pageForcedModGroups.tbCalorieCount});
            }
            else if (_currentPage is PageProducts)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pageProducts.tbGroupName, 
                    _pageProducts.tbGroupPrice,
                    _pageProducts.tbGroupLocWeight,
                    _pageProducts.tbDownTick,   
                    _pageProducts.tbName,
                    _pageProducts.tbPriceTextBox, 
                    _pageProducts.textBoxUPC, 
                    _pageProducts.textBoxSku, 
                    _pageProducts.tbForcedModMenuWeight,
                    _pageProducts.tbCalorieCount});
            }
            else if (_currentPage is PagePriceIntervals)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pagePriceIntervals.tbTextBoxCompleteName, _pagePriceIntervals.tbPriceTextBox });
            }
            else if (_currentPage is PagePrinters)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pagePrinters.tbPrinterSymbolicName, _pagePrinters.tbGroupName });
            }
            else if (_currentPage is PageMenus)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pageMenus.tbGroupName, _pageMenus.tbMenuName });
            }
            else if (_currentPage is PageSystemConfiguration)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control>{_pageSystemConfig.tbReceipt1,_pageSystemConfig.tbReceipt2,_pageSystemConfig.tbReceipt3,
                    _pageSystemConfig.tbReceipt4, _pageSystemConfig.tbTopMessage, _pageSystemConfig.tbBottomMessage});
            }
            else if (_currentPage is PageKiosks)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pageNodes.tbName, 
                        _pageNodes.tbCashDrawerCount, 
                        _pageNodes.tbFirstCashDrawerPinNum, _pageNodes.tbcashcopies, _pageNodes.tbcreditcopies});
            }
            else if (_currentPage is PageLayout)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe();
            }
            else if (_currentPage is PageCompReason)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pageCompReason.textBoxCompleteName, _pageCompReason.tbRate });

            }
            else if (_currentPage is PageDayPart)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { _pageDayParts.textBoxCompleteName, _pageDayParts.textBoxDaypartOridinal });
            }
            else if (_currentPage is PageHouseAccount)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> {
                        _pageHouseAccount.tblastNameTextBox, 
                        _pageHouseAccount.tbfirstNameTextBox, 
                        _pageHouseAccount.tbCompanyNameTextBox,
                        _pageHouseAccount.tbAccountLimit, 
                        _pageHouseAccount.tbTermsAndConditions, 
                        _pageHouseAccount.tbcityTextBox, 
                        _pageHouseAccount.tbstateTextBox, 
                        _pageHouseAccount.tbzipcodeTextBox,
                        _pageHouseAccount.tbaddress1TextBox, 
                        _pageHouseAccount.tbaddress2TextBox, 
                        _pageHouseAccount.tbEmailAddress,
                        _pageHouseAccount.tbtelephoneTextBox,
                        _pageHouseAccount.tbFax, 
                        _pageHouseAccount.tbTaxIdTextBox});
            }
            //else if (_currentPage is PageVendorAccount)
            //{
            //    Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
            //    Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
            //        new List<Control> { _pageVendorAccount.tblastNameTextBox, _pageVendorAccount.tbfirstNameTextBox, _pageVendorAccount.tbCompanyNameTextBox,
            //        _pageVendorAccount.tbAccountLimit, _pageVendorAccount.tbTermsAndConditions, _pageVendorAccount.tbcityTextBox, _pageVendorAccount.tbstateTextBox, 
            //        _pageVendorAccount.tbzipcodeTextBox,_pageVendorAccount.tbaddress1TextBox, _pageVendorAccount.tbaddress2TextBox, _pageVendorAccount.tbEmailAddress,
            //        _pageVendorAccount.tbtelephoneTextBox,_pageVendorAccount.tbFax, _pageVendorAccount.tbTaxIdTextBox});
            //}
            else if (_currentPage is PageAutoGratuity)
            {
                Oceanside.FullKeyboard.ActiveKeyboard.SetSmallMode();
                Oceanside.FullKeyboard.ActiveKeyboard.ShowMe(
                    new List<Control> { 
                        _pageAutoGratuity.textBoxName, 
                        _pageAutoGratuity.textBoxPercent, 
                        _pageAutoGratuity.textBoxPartyCount,
                        _pageAutoGratuity.textboxAboveTicketAmount});
            }

            if (_currentPage is PageLogin && !isLoggedIn)
            {
                (_pageLogin as IPageToImp).PerformLogin();
            }
            else
            {
                (_currentPage as IPageToImp).KeyBoardHiddenCallback_NOTHROW(Oceanside.FullKeyboard.LastResponse);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tc"></param>
        public static void AddInstanceToTabControl(ref TabControl tc)
        {
            tc.Items.Add(singleton);
        }

        /// <summary>
        /// 
        /// </summary>
        public static void Show()
        {
            SwitchWindowDelegate_NOTHROW(singleton);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        public static void SetTabIndex(int index)
        {
            singleton.MyTabIndex = index;
        }

        private void buttonNewGroup_Click(object sender, RoutedEventArgs e)
        {
            (_currentPage as IPageToImp).NewGroupClicked();
        }

        private void buttonDeleteGroup_Click(object sender, RoutedEventArgs e)
        {
            (_currentPage as IPageToImp).DeleteGroupClicked();
        }

        private void buttonNewEntity_Click(object sender, RoutedEventArgs e)
        {
            (_currentPage as IPageToImp).NewEntityClicked();
        }

        private void buttonDeleteEntity_Click(object sender, RoutedEventArgs e)
        {
            (_currentPage as IPageToImp).DeleteEntityClicked();
        }

        private void buttonEdit_Click(object sender, RoutedEventArgs e)
        {
            (_currentPage as IPageToImp).RefreshClicked();
            (_currentPage as IPageToImp).EditFormClicked();
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            (_currentPage as IPageToImp).CancelClicked();
        }

        private void buttonSaveChanges_Click(object sender, RoutedEventArgs e)
        {
            (_currentPage as IPageToImp).SaveClicked();
        }
    }
}
